//
//  WWShoppingCarCell.m
//  Shopping
//
//  Created by 张世敏 on 16/8/20.
//  Copyright © 2016年 张湾湾. All rights reserved.
//

#import "WWShoppingCarCell.h"
#import "WWRightCellModel.h"

@interface WWShoppingCarCell()

/** 商品名称 **/
@property (nonatomic, strong) UILabel *nameLabel;
/** 商品价格 **/
@property (nonatomic, strong) UILabel *priceLabel;
/** 当前商品购买的数量 **/
@property (nonatomic, strong) UILabel *currentGoodsCountLabel;
///** 价格总和 **/
//@property (nonatomic, assign) CGFloat *totalCost;

@end

@implementation WWShoppingCarCell

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    
    return self;
}


#pragma mark - 点击"+"或"-"按钮
-(void)changeCurrentGoodsCount:(UIButton *)sender{

    NSUInteger currentCount = self.rightCellModel.userBuyCount; // 当前商品的购买数量
    
    // tag值2表示增加,1表示减少
    if (sender.tag == 2) {
        // 1.同步到服务器
        // 2.更新购物车缓存数组
        [[WWShopCarManager sharedShopCarManager] addSupermarketGoodsToShopCar:self.rightCellModel];
        // 3.更新当前页面
        currentCount++;
        self.currentGoodsCountLabel.text = [NSString stringWithFormat:@"%@",@(currentCount)];
        
    } else {
        // 如果本身为0,则不能再减1了
        if (currentCount == 0) {
            return;
        }
        
        [[WWShopCarManager sharedShopCarManager] removeSupermarketGoodsFromShopCar:self.rightCellModel];
        currentCount--;
        
        if (currentCount > 0) {
            self.currentGoodsCountLabel.text = [NSString stringWithFormat:@"%@",@(currentCount)];
        } else {
            // 当前购买的商品数量为0,移除cell
            self.currentGoodsCountLabel.text = [NSString stringWithFormat:@"%@",@(0)];
        }
    }
    
    // 计算总价
    if (self.changeTotalCostBlock) {
        _changeTotalCostBlock(self.rightCellModel.price.floatValue * (sender.tag == 2 ? 1 : -1));
    }
}


/** 给cell赋值 **/
-(void)setRightCellModel:(WWRightCellModel *)rightCellModel{
    _rightCellModel = rightCellModel;
    
    self.nameLabel.text = rightCellModel.name;
    self.priceLabel.text = [NSString stringWithFormat:@"$%@",rightCellModel.price];
    self.currentGoodsCountLabel.text = [NSString stringWithFormat:@"%@",@(rightCellModel.userBuyCount)];
}


/** 设置界面 **/
-(void)setupUI{
    // 添加子控件
    self.nameLabel = [self addLabelWithFontSize:15 textAlignment:NSTextAlignmentLeft];
    self.priceLabel = [self addLabelWithFontSize:17 textAlignment:NSTextAlignmentRight];
    self.currentGoodsCountLabel = [self addLabelWithFontSize:14 textAlignment:NSTextAlignmentCenter];
    
    UIButton *increaseBtn = [self addButtonWithImageNamed:@"v2_increase" isIncrease:YES];
    UIButton *reduceBtn = [self addButtonWithImageNamed:@"v2_reduce" isIncrease:NO];
    
    // 测试数据
//    self.nameLabel.text = @"[精选] 爱鲜蜂.爱鲜蜂.爱鲜蜂.爱鲜蜂";
//    self.priceLabel.text = [NSString stringWithFormat:@"$%@",@7.9];
//    self.currentGoodsCountLabel.text = [NSString stringWithFormat:@"%@",@0];
    
    // 布局
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(0);
        make.leading.offset(17);
        make.width.lessThanOrEqualTo(SCREENW * 0.5);
    }];
    
    // "+"
    [increaseBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel);
        make.trailing.offset(-5);
        make.size.equalTo(CGSizeMake(30, 30));
    }];
    
    // 数量
    [self.currentGoodsCountLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel);
        make.trailing.equalTo(increaseBtn.leading).offset(-2);
        make.width.equalTo(23);
    }];
    
    // "-"
    [reduceBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel);
        make.trailing.equalTo(self.currentGoodsCountLabel.leading).offset(-2);
        make.size.equalTo(increaseBtn);
    }];
    
    // 价格
    [self.priceLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel);
        make.trailing.equalTo(reduceBtn.leading).offset(-5);
        make.width.lessThanOrEqualTo(80);
    }];
}


/** 添加label **/
-(UILabel *)addLabelWithFontSize:(CGFloat)fontSize textAlignment:(NSTextAlignment)textAlignment{
    UILabel *label = [UILabel new];
    label.font = [UIFont systemFontOfSize:fontSize];
    label.textAlignment = textAlignment;
    label.preferredMaxLayoutWidth = SCREENW * 0.5;
    [self.contentView addSubview:label];
    
    return label;
}


/** 添加加号或减号按钮 **/
-(UIButton *)addButtonWithImageNamed:(NSString *)imageName isIncrease:(BOOL)isIncrease{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = isIncrease ? 2 : 1; // 2表示增加,1表示减少
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(changeCurrentGoodsCount:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn];
    
    return btn;
}

@end
